package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ApprovalMapper;
import com.itheima.pojo.Approval;
import com.itheima.pojo.Result;
import com.itheima.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper, Approval> implements ApprovalService {
    @Autowired
    private ApprovalMapper approvalMapper;

    @Override
    public Result<Page> selectPage(Page page, String applicant, Integer type, Integer status,Integer applicantId) {
        // 创建LambdaQueryWrapper来构造查询条件
        LambdaQueryWrapper<Approval> approvalLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 进行模糊查询，查询条件是申请人（applicant）
        if (StringUtils.isNotBlank(applicant)) {
            approvalLambdaQueryWrapper.like(Approval::getApplicant, applicant);
        }
        // 查询条件是审批类型（type）
        if (type!=null) {
            approvalLambdaQueryWrapper.eq(Approval::getType, type);
        }
        // 查询条件是审批状态（status）
        if (status!=null) {
            approvalLambdaQueryWrapper.eq(Approval::getStatus, status);
        }
        // 查询条件是申请人id（applicantId）
        if (applicantId != null) {
            approvalLambdaQueryWrapper.eq(Approval::getApplicantId, applicantId);
        }

        // 调用Mapper的selectPage方法进行分页查询
        Page page1 = approvalMapper.selectPage(page, approvalLambdaQueryWrapper);
        return Result.success(page1);
    }

}
